import matplotlib.pyplot as plt

# 数据
years = ['2022', '2023']
cash_flow = [9.56, 14.08]  # 经营活动产生的现金流量净额（单位：亿元）
roe = [13.54, 7.36]  # 净资产收益率（单位：%）
gross_margin = [28.90, 31.51]  # 销售毛利率（单位：%）
net_profit_margin = [2.73, 1.42]  # 销售净利率（单位：%）

# 设置颜色
colors = ['blue', 'green', 'red', 'purple']

# 创建图表
fig, ax1 = plt.subplots()

# 条形图：经营活动产生的现金流量净额
bars = ax1.bar(years, cash_flow, color=colors[0], label='Net Cash Flow from Operating Activities')

# 折线图：净资产收益率（ROE）
line_roe, = ax1.plot(years, roe, color=colors[1], marker='o', label='ROE')

# 折线图：销售毛利率
line_gross, = ax1.plot(years, gross_margin, color=colors[2], marker='s', label='Gross Margin')

# 折线图：销售净利率
line_net, = ax1.plot(years, net_profit_margin, color=colors[3], marker='^', label='Net Profit Margin')

# 在条形图上显示数据
for bar in bars:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2.0, yval, f'{yval}B', ha='center', va='bottom')

# 设置标题和轴标签
plt.title('Financial Performance Comparison (2022-2023)')
plt.xlabel('Year')
plt.ylabel('Amount (Unit: Billion Yuan) / Percentage (%)')

# 创建第二个y轴
ax2 = ax1.twinx()

# 折线图的y轴
ax2.plot(years, roe, color=colors[1], marker='o', label='ROE')
ax2.plot(years, gross_margin, color=colors[2], marker='s', label='Gross Margin')
ax2.plot(years, net_profit_margin, color=colors[3], marker='^', label='Net Profit Margin')

# 在折线图上显示数据
for i, txt in enumerate(roe):
    ax2.annotate(f'{txt}%', (years[i], roe[i]), textcoords="offset points", xytext=(0,10), ha='center')
for i, txt in enumerate(gross_margin):
    ax2.annotate(f'{txt}%', (years[i], gross_margin[i]), textcoords="offset points", xytext=(0,10), ha='center')
for i, txt in enumerate(net_profit_margin):
    ax2.annotate(f'{txt}%', (years[i], net_profit_margin[i]), textcoords="offset points", xytext=(0,10), ha='center')

ax2.set_ylabel('Percentage (%)')

# 图例
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper left')

# 显示图形
plt.show()